package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Api(tags = "分类相关注解")
@Slf4j
public class CategoryController
{
    @Autowired
    CategoryService categoryService;

    @PutMapping
    @ApiOperation("修改分类")
    public Result update(@RequestBody CategoryDTO CategoryDTO){
        log.info("修改分类{}",CategoryDTO);
        categoryService.update(CategoryDTO);
        return Result.success();
    }

    @GetMapping("page")
    @ApiOperation("分页查询")
    public Result find( CategoryPageQueryDTO CategoryPageQueryDTO){
        log.info("分页查询{}",CategoryPageQueryDTO);
        return Result.success(categoryService.find(CategoryPageQueryDTO));
    }

    @PostMapping("status/{status}")
    @ApiOperation("启用、禁用分类")
    public Result updateStatus(@PathVariable("status") Integer status,Long id){
        log.info("启用、禁用分类{},id:{}",status,id);
        categoryService.updateStatus(status,id);
        return Result.success();
    }

    @ApiOperation("新增分类")
    @PostMapping
    public Result add(@RequestBody CategoryDTO CategoryDTO){
        log.info("新增分类{}",CategoryDTO);
        categoryService.add(CategoryDTO);
        return Result.success();
    }


    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result delete( Long id){
        log.info("根据id删除分类{}",id);
        categoryService.delete(id);
        return Result.success();
    }

    @ApiOperation("根据类型查询分类")
    @GetMapping("list")
    public Result findByType(Integer Type){
        log.info("根据类型查询分类{}",Type);
        return Result.success(categoryService.findByType(Type));
    }
}
